package zy.blue7.im.core.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import zy.blue7.im.common.message.MessageInfo;
import zy.blue7.im.core.utils.SerializableUtils;

/**
 * @Author: blue7
 * @Date: 2022/2/27 18:58
 * @Version 1.0
 * @Description
 */
public class MessageInfoEncoder extends MessageToByteEncoder<MessageInfo> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, MessageInfo messageInfo, ByteBuf byteBuf) throws Exception {
        byteBuf.writeInt(messageInfo.getHeader().getVersion());
        byteBuf.writeInt(messageInfo.getHeader().getContentLength());
        byteBuf.writeBytes(messageInfo.getHeader().getSessionId().getBytes());


        byte[] contentBytes = SerializableUtils.serializable(messageInfo.getMessage());
        byteBuf.writeBytes(contentBytes);

    }
}
